<?php
/**
 * 视频列表
 * @author Hongwei 2017-06-26
 */
namespace Videos\Controller;
use Common\Controller\AdminbaseController;

class AdminVideosController extends AdminbaseController{

	protected $videos_model;
	protected $videos_cate_model;

	function _initialize()
	{
		parent::_initialize();
		$this->videos_model = D("Videos");
		$this->videos_cate_model = D("VideosCate");
	}

	// 全部列表,不按分类
    public  function videoslist()
    {
        //条件-名称
        $videos_title = I('videos_title');
        if($videos_title){
            $where['videos_title'] = array('like',"%$videos_title%");
        }

        //数量
        $count=$this->videos_model->where($where)->count();

        //分页
        $page = $this->page($count, 10);

        $db_prefix=C('DB_PREFIX');
        $lists = $this->videos_model->where($where)->select();

        $this -> assign("page", $page->show('Admin'));
        $this -> assign('lists',$lists);
        $this->display('index');

    }
	// 视频分类列表
	public function index(){

		//分类
		$cate = $this->videos_cate_model
			->order('orderby asc')
			->select();
		$this->assign('cate',$cate);

		//条件-名称
		$videos_title = I('videos_title');
		if($videos_title){
			$where['videos_title'] = array('like',"%$videos_title%");
		}

		//条件-分类
		$videos_cid = I('videos_cid');
		if($videos_cid) {
			$where['videos_cid'] = $videos_cid;
		}
		$this->assign('videos_cid',$videos_cid);	//分类选中值


		//数量
		$count=$this->videos_model->where($where)->count();

		//分页
		$page = $this->page($count, 10);

		$db_prefix=C('DB_PREFIX');
		$lists = $this->videos_model
			-> alias('a')
			-> field('a.*,b.cate_name')
			-> join($db_prefix.'videos_cate  b  ON a.videos_cid = b.cate_id')
			-> where($where)
			-> order('a.orderby asc,videos_id desc')
			-> limit($page->firstRow, $page->listRows)
			-> select();
		$this -> assign("page", $page->show('Admin'));
		$this -> assign('lists',$lists);
		$this->display('index');
	}



//填加
	public function add() {
		$this -> _modify();
	}

	//修改
	public function edit() {
		$this -> _modify();
	}


	private function _modify() {

		$videos_id = I('videos_id');

		if (IS_POST) {
			$validate = array(
				array('videos_title', 'require', '视频标题不能为空'),
			);

			$result = array('status' => TRUE, 'info' => '', 'url' => '');

			$this->videos_model -> field('videos_cid,tid,videos_title,videos_thumb,videos_mp4,orderby');

			if ($this->videos_model -> validate($validate) -> create()) {
				if (!$videos_id) {
					$insert_id = $this->videos_model -> add();
					if ($insert_id) {
						$result['info'] = '添加成功';
					} else {
						$result['status'] = FALSE;
						$result['info'] = '添加失败';
					}
				} else {
					if($this->videos_model -> where(array('videos_id' => $videos_id)) -> save()) {
						$result['info'] = '保存成功';
					} else {
						$result['status'] = FALSE;
						$result['info'] = '保存失败';
					}
				}
			} else {
				//返回错误提示
				$result['status'] = FALSE;
				$result['info'] = $this->videos_model -> getError();
			}

			if ($result['status']) {
				$this -> success($result['info'],U('AdminVideos/index'));
			} else {
				$this -> error($result['info']);
			}
		}

		//单个值
		$videos =  $this->videos_model -> find($videos_id);
		$this -> assign('videos', $videos);
		//分类
		$select_cates = $this->videos_cate_model -> select();
		$this -> assign('select_cates', $select_cates);
		
		//老师列表
		$teacher = M('teacher_application')->where("state = '1'")->order("id desc")->select();
		$this->assign("teacher",$teacher);
		
		//模板
		$this -> display('AdminVideos/form');
	}

	// 删除
	public function delete() {
		$videos_id = I('videos_id/d');
		$where['videos_id'] = $videos_id;
		$result = $this->videos_model->where($where)->delete();
		$data = array(
			'status' => $result ? true : false,
			'info' => $result ? '' : '删除失败，ID：'.$videos_id,
			'url' => '',
		);
		$this -> ajaxReturn($data);
	}

	/**
	 * 批量操作
	 * @param string $type    处理类型 order排序
	 */
	public function indexBat($type='delete') {
		// 批量删除
		if ($type == 'delete') {
			$ids = I('post.ids');
			if ($this->videos_model -> delete(implode(',',$ids)) !== FALSE) {
				$this -> success('批量删除成功');
			} else {
				$this -> error('删除失败');
			}
		}
		//排序
		if ($type == 'order') {
			$order_array = I('post.orderby');
			if ($this->videos_model -> saveBatch($order_array, 'videos_id', 'orderby') !== FALSE) {
				$this -> success('排序成功');
			} else {
				$this -> error('排序出错');
			}
		}

	}


}